# -*- coding: utf-8 -*-
# @Time    : 2021/10/20
# @Author  : CHENlj
# @File    : Request.py

"""
封装request

"""
import requests
from Common.Log import Mylog
from Common.ApiAdress import HOST

import json


class Request:

    def __init__(self):
        pass

    @staticmethod
    def get_request(url, params=None, headers=None, cookies=None):
        """
        发起get请求
        :param url: 必填
        :param params: 选填
        :param headers: 选填
        :param cookies: 选填
        :return:
        """
        # if not url.startswith('http://'):
        #     url = "%s%s" % ("http://",url)
        #     print(url)

        try:
            if params is None:
                responce = requests.get(HOST+url, headers=headers, cookies=cookies)

            else:
                responce = requests.get(HOST+url, params=params, headers=headers, cookies=cookies)

        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', HOST+url))
            Mylog.info(e)
            return ()

        # total_seconds为响应时间，单位为秒
        total_seconds = responce.elapsed.total_seconds()

        response_dicts = dict()
        try:
            response_dicts['body'] = responce.json()
        except Exception as e:
            print(e)
            response_dicts['body'] = ''
        response_dicts['code'] = responce.status_code
        response_dicts['response_text'] = responce.text
        response_dicts['total_seconds'] = total_seconds
        response_dicts['responce'] = responce

        return responce

    @staticmethod
    def post_request(url, data=None, json=None, headers=None, cookies=None):
        """
        发起post请求
        :param url:
        :param data:
        :param json:
        :param headers:
        :param cookies:
        :return:
        """
        # if not url.startswith("http://"):
        #     url = "%s%s" % ("http://",url)

        try:
            responce = requests.post(HOST+url, data=data, json=json, headers=headers, cookies=cookies)
        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', HOST+url))
            Mylog.info(e)
            return ()

        # total_seconds为响应时间，单位为秒
        total_seconds = responce.elapsed.total_seconds()

        response_dicts = dict()
        response_dicts['status_code'] = responce.status_code
        try:
            response_dicts['body'] = responce.json()
        except Exception as e:
            print(e)
            response_dicts['body'] = ''

        response_dicts['response_text'] = responce.text
        response_dicts['total_seconds'] = total_seconds

        return responce

    @staticmethod
    def put_request(url, params=None, data=None, json=None, headers=None):
        """
        发起put请求
        :param url:
        :param params:
        :param data:
        :param headers:
        :return:
        """
        try:
            response = requests.put(HOST+url, params=params, data=data, json=json, headers=headers)
        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', HOST+url))
            Mylog.info(e)
            return ()

        return response

    @staticmethod
    def del_requests(url, headers=None):
        try:
            response = requests.delete(HOST+url, headers=headers)
        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', HOST+url))
            Mylog.info(e)
            return ()

        return response


if __name__ == "__main__":
    pass







